<?php

namespace app\admin\controller;

use think\Lang;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Invoice extends AdminControl
{
    const EXPORT_SIZE = 1000;

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/refund.lang.php');
    }


    /**
     * 发票类型列表
     */
    public function index()
    {
        $Invoice_model = model('Invoice');
        $condition = array();

        if (trim(input('param.title'))) {
            $title_name = input('param.title');
            $condition['name'] = array('like', "%$title_name%");
        }
        $type = intval(input('param.type'));
        if ($type) {
            $condition['type'] = $type;
        }

        $status = intval(input('param.status'));
        if ($status) {
            if ($status == 2) {
                $condition['status'] = 0;
            } else {
                $condition['status'] = $status;
            }

        }


        /** 新增限制条件
         * */
        $condition['is_del'] = 0;
        $list = $Invoice_model->getInvoiceList($condition, 10);
        $this->assign('list', $list);
        $this->assign('show_page', $Invoice_model->page_info->render());
        $this->assign('title', input('param.title'));
        $this->assign('type', $type);
        $this->assign('status', $status);
        $this->setAdminCurItem('index');
        return $this->fetch('index');
    }

    /**
     * 新增发票类型
     */
    public function add()
    {
        if (request()->post()) {
            $new_array = array();
            $new_array['name'] = input('post.name');
            $new_array['sort'] = intval(input('post.sort'));
            $new_array['type'] = input('post.type');
            $new_array['status'] = intval(input('post.status'));
            $new_array['addtime'] = time();
            $new_array['billing_type'] = input('post.billing_type');
            $state = db('invoice_type')->data($new_array)->insert();
            if ($state) {
                $this->log('新增发票原因，编号' . $state);
                dsLayerOpenSuccess(lang('ds_common_save_succ'));
            } else {
                dsLayerOpenSuccess('添加失败');
            }
        }
        return $this->fetch('add');
    }

    /**
     * 编辑发票类型
     *
     */
    public function edit()
    {
        $condition = array();
        $id = intval(input('param.id'));
        $info = db('invoice_type')->where('id', $id)->where('is_del', 0)->find();
        if(empty($info)){
            dsLayerOpenSuccess('操作失败');
        }
        if (request()->post()) {
            $new_array = array();
            $new_array['name'] = input('post.name');
            $new_array['sort'] = intval(input('post.sort'));
            $new_array['type'] = input('post.type');
            $new_array['status'] = intval(input('post.status'));
            $new_array['billing_type'] = intval(input('post.billing_type'));
            $state = db('invoice_type')->where('id',$id)->data($new_array)->update();
            if ($state) {
                $this->log('编辑发票类型，编号' . $id);
                dsLayerOpenSuccess(lang('ds_common_save_succ'));
            } else {
                dsLayerOpenSuccess('编辑失败');
            }
        }
        $this->assign('list', $info);
        return $this->fetch('edit');
    }

    /**
     *  退换货理由上下架
     */

    public function status()
    {
        $id = trim(input('param.id'));
        $status = trim(input('param.status'));
        $info = db('invoice_type')->where('id', $id)->where('is_del', 0)->find();
        if ($status == 0) {
            #操作下架 判断是否是最后一条
            $count = db('invoice_type')->where('status', 1)->where('is_del', 0)->count();
            if ($count == 1) {
                ds_json_encode(10001, '不能停用最后一条');
            }
        }
        if (empty($id)) {
            ds_json_encode(10001, '操作失败');
        }

        if ($status == $info['status']) {
            ds_json_encode(10001, '操作失败');
        }
        $data = array();
        $data['status'] = $status;
        $rs = db('invoice_type')->where('id', $id)->data($data)->update();
        if ($rs) {
            $this->log(lang('ds_del') . '发票类型' . '[ID:' . $id . ']');
            ds_json_encode(10000, '操作成功');
        } else {
            ds_json_encode(10001, '操作失败');
        }
    }

    /**
     * 删除退款退货原因
     *
     */
    public function del()
    {
        $id = input('param.id');

        $invoice_info = db('invoice')->where(['invoice_type'=>$id])->select();
        if($invoice_info){
            ds_json_encode(10001, '当前发票类型已绑定，暂时不能删除');
        }
        if (empty($id)) {
            ds_json_encode(10001, '操作失败');
        }
        $info = db('invoice_type')->where('is_del', 0)->where('id', $id)->find();
        if (empty($info)) {
            ds_json_encode(10001, '操作失败');
        }
        if ($info['status'] == 1) {
            ds_json_encode(10001, '请先停用');
        }
        $rs = db('invoice_type')->where('id', $id)->data(['is_del' => 1])->update();
        if ($rs) {
            $this->log('删除发票类型，编号' . $id);
            ds_json_encode('10000', lang('ds_common_del_succ'));
        } else {
            ds_json_encode('10001', lang('ds_common_del_fail'));
        }
    }


// 发票列表

public function invoicelist()
{
    $Invoice_model = model('Invoice');
    $condition = array();

    if (trim(input('param.title'))) {
        $title_name = input('param.title');
        $condition['name'] = array('like', "%$title_name%");
    }
    $type = intval(input('param.type'));
    if ($type) {
        $condition['type'] = $type;
    }

    $status = intval(input('param.status'));
    if ($status) {
        if ($status == 2) {
            $condition['status'] = 0;
        } else {
            $condition['status'] = $status;
        }
    }

    $list = $Invoice_model->getInvoiceList2($condition, 10);
    $this->assign('list2', $list);
    $this->assign('show_page', $Invoice_model->page_info->render());
    $this->assign('title', input('param.title'));
    $this->assign('type', $type);
    $this->setAdminCurItem('invoicelist');
    return $this->fetch('invoicelist');
}

// 发票列表

public function invoicerecord()
{
    $Invoice_model = model('Invoice');
    $condition = array();

    if (trim(input('param.title'))) {
        $title_name = input('param.title');
        $condition['name'] = array('like', "%$title_name%");
    }
    $type = intval(input('param.type'));
    if ($type) {
        $condition['type'] = $type;
    }

    $status = intval(input('param.status'));
    if ($status) {
        if ($status == 2) {
            $condition['status'] = 0;
        } else {
            $condition['status'] = $status;
        }
    }

    $list = $Invoice_model->getInvoiceList3($condition, 10);
    // var_dump($list);die;
    $this->assign('list2', $list);
    $this->assign('show_page', $Invoice_model->page_info->render());
    $this->assign('title', input('param.title'));
    $this->assign('type', $type);
    $this->setAdminCurItem('invoicerecord');
    return $this->fetch('invoicerecord');

}



    /**
     * 发票详情
     *
     */
    public function list1()
    {
        $id= intval(input('param.id'));
        $tblname='invoice';
        $field='a.*,b.name,b.type,b.billing_type';
        $where['a.invoice_id']=$id;
        $where['b.status']=1;
        $where['b.is_del']=0;
        $invoice_info = db ( $tblname . ' a' )->join ( '__INVOICE_TYPE__ b', 'a.invoice_type_id=b.id' )->where ( $where )->field ($field) ->find();
        $this->assign('invoice_info', $invoice_info);
        return $this->fetch('list');
    }
        /**
     * 发票详情
     *
     */
    public function list2()
    {
        $id= intval(input('param.id'));
        $tblname='invoice';
        $field='a.*,b.name,b.type,b.billing_type,ao.*';
        $where['a.invoice_id']=$id;
        $where['b.status']=1;
        $where['b.is_del']=0;
        $invoice_info = db ( $tblname . ' a' )
        ->join ( '__INVOICE_TYPE__ b', 'a.invoice_type_id=b.id' )
        ->join ( 'app_order ao', 'ao.app_order_id=a.app_order_id' )
        ->where ( $where )->field ($field) ->find();
        $this->assign('invoice_info', $invoice_info);
        return $this->fetch('list2');
    }





    /**
     * 获取卖家栏目列表,针对控制器下的栏目
     */
    protected function getAdminItemList()
    {
        $menu_array = array(
               array( 'name' => 'invoicelist', 'text' => '发票列表', 'url' => url('Invoice/invoicelist'),
            ),
               array( 'name' => 'invoicerecord', 'text' => '发票记录', 'url' => url('Invoice/invoicerecord'),
            ),
             array( 'name' => 'index', 'text' => '发票类型管理', 'url' => url('Invoice/index'),
            ),
            array(
                'name' => 'add_reason', 'text' => '新增发票类型', 'url' => "javascript:dsLayerOpen('" . url('Invoice/add') . "','" . '添加发票类型'. "')"

            ),
        );
   
        return $menu_array;
    }
}

?>
